﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Library;

namespace OA.Exam
{
    public partial class MarkPaper : System.Web.UI.Page
    {
        Exam myExam = new Exam();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["examid"] != null)
                {
                    string examId = Request["examid"].ToString();
                    string paperId = myExam.GetRecordOfExam(examId).Rows[0]["paperid"].ToString();
                    string status = myExam.GetRecordOfExam(examId).Rows[0]["status"].ToString();
                    HiddenExamId.Value = examId;
                    HiddenPaperId.Value = paperId;

                    //试卷名称，总分
                    LabPaperName.Text = myExam.GetRecordOfPaper(paperId).Rows[0]["papername"].ToString();
                    LabScore.Text = myExam.GetRecordOfPaper(paperId).Rows[0]["score"].ToString();
                    //考生，得分，评论
                    LabUserName.Text = myExam.GetRecordOfExam(examId).Rows[0]["UserName"].ToString();
                    LabMyScore.Text = myExam.GetRecordOfExam(examId).Rows[0]["score"].ToString();
                    TxtComment.Text = myExam.GetRecordOfExam(examId).Rows[0]["comment"].ToString();
                    if (LabMyScore.Text == "0")
                    {
                        LabMyScore.Text = "未打分";
                    }
                    //绑定大题
                    BindBig(paperId);
                    
                    //绑定题目答案
                    myExam.BindHadSaveAnswer(DataListBigQuestion, examId);
                    //绑定已打分
                    BindHadMarkScore();
                    if (status == "2")
                    {
                        LabPrompt.Text = "批阅已结束";
                        BtnLogin.Visible = false;
                        BtnEndMark.Visible = false;
                    }
                }
            }
        }

        protected void BindBig(string paperId)
        {
            string sql = "select ItemType,Score,TypeName from Exam_PapersQuestionTypes as a left join Exam_QuestionTypes as b on a.ItemType=b.TypeId where PaperId='" + paperId + "' order by DispOrder";
            DataListBigQuestion.DataSource = DataAccess.getDataTable(sql);
            DataListBigQuestion.DataBind();
        }

        #region 绑定大题
        protected void DataListBigQuestion_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //大题编号处理
                Label LabNum = (Label)e.Item.FindControl("LabNum");
                switch (LabNum.Text)
                {
                    case "1":
                        LabNum.Text = "一";
                        break;
                    case "2":
                        LabNum.Text = "二";
                        break;
                    case "3":
                        LabNum.Text = "三";
                        break;
                    case "4":
                        LabNum.Text = "四";
                        break;
                    case "5":
                        LabNum.Text = "五";
                        break;
                }

                //题目说明处理
                string itemType = DataListBigQuestion.DataKeys[e.Item.ItemIndex].ToString();
                Label LabDescription = (Label)e.Item.FindControl("LabDescription");
                Label LabScore = (Label)e.Item.FindControl("LabScore");
                switch (itemType)
                {
                    case "00001":
                        LabDescription.Text = "（请在每题后面选择正确还是错误。";
                        break;
                    case "00002":
                        LabDescription.Text = "（下列每题备选答案中，只有一个符合题意的正确答案。请在选项中选择。";
                        break;
                    case "00003":
                        LabDescription.Text = "（下列每题备选答案中，有两个或两个以上符合题意的正确答案。请在选项中选择。";
                        break;
                    case "00004":
                        LabDescription.Text = "（下列每题中（）代表需填空的位置，请在对应的矩形框中填写答案。";
                        LabScore.Text = "）";
                        break;
                    case "00005":
                        LabDescription.Text = "（请在每题对应的矩形框中填写答案。";
                        LabScore.Text = "）";
                        break;
                }
                //绑定小题
                DataList DataListQuestions = e.Item.FindControl("DataListQuestions") as DataList;
                DataListQuestions.DataSource = myExam.GetSmallQuestions(HiddenPaperId.Value, itemType);
                DataListQuestions.DataBind();
            }
        }
        #endregion

        #region 绑定小题
        protected void DataListQuestions_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                //string questionId = DataListBigQuestion.DataKeys[((DataListItem)((e.Item.NamingContainer).NamingContainer)).ItemIndex].ToString();
                HiddenField HfQuestionId = (HiddenField)e.Item.FindControl("HfQuestionId");
                string questionId = HfQuestionId.Value;
                HiddenField HfItemType = (HiddenField)e.Item.FindControl("HfItemType");
                Label LabSubject = (Label)e.Item.FindControl("LabSubject");

                //haha
                //Label LabQNum = (Label)e.Item.FindControl("LabQNum");
                //Label LabNumMark = (Label)((DataListItem)e.Item.NamingContainer.NamingContainer).FindControl("LabNumMark");
                switch (HfItemType.Value)
                {
                    case "00002":
                        DataList DataListRadio = e.Item.FindControl("DataListRadio") as DataList;
                        DataListRadio.DataSource = myExam.GetItemListOfQuestion(questionId);
                        DataListRadio.DataBind();
                        break;
                    case "00003":
                        DataList DataListMultiple = e.Item.FindControl("DataListMultiple") as DataList;
                        DataListMultiple.DataSource = myExam.GetItemListOfQuestion(questionId);
                        DataListMultiple.DataBind();
                        break;
                    case "00004":
                        string[] arr = LabSubject.Text.Trim().Split('（');
                        int textNum = arr.Length - 1;
                        //HtmlGenericControl DivBlank = new HtmlGenericControl("div");
                        //采用声明方式添加文本框，根据需要显示适当的数量
                        for (int i = 1; i <= textNum; i++)
                        {
                            TextBox BlankTextBox = (TextBox)e.Item.FindControl("BlankTextBox" + i.ToString());
                            BlankTextBox.Visible = true;
                        }
                        break;
                }
            }
        }
        #endregion
        //提交
        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            int singleScore, objectiveScore;
            int totalScore = 0;
            int i, j;
            string correctAnswer = string.Empty;                         //标准答案
            string myAnswer = string.Empty;                              //考生答案
            for (i = 0; i < DataListBigQuestion.Items.Count; i++)
            {
                string itemType = DataListBigQuestion.DataKeys[i].ToString();
                objectiveScore = int.Parse(((HiddenField)DataListBigQuestion.Items[i].FindControl("HfScore")).Value);//客观题每题分值
                DataList DataListQuestions = DataListBigQuestion.Items[i].FindControl("DataListQuestions") as DataList;
                switch (itemType)
                {
                    case "00001":        /*主观题*/
                    case "00002":
                    case "00003":
                        for (j = 0; j < DataListQuestions.Items.Count; j++)
                        {
                            string questionId = DataListQuestions.DataKeys[j].ToString();
                            correctAnswer = myExam.GetRecordOfQuestion(questionId).Rows[0]["Answer"].ToString().Trim();
                            myAnswer = myExam.GetRecordOfAnswer(HiddenExamId.Value, questionId).Rows[0]["AnswerContent"].ToString().Trim();
                            if (correctAnswer == myAnswer)
                            {
                                totalScore += objectiveScore;
                            }
                        }
                        break;
                    case "00004":
                    case "00005":
                        for (j = 0; j < DataListQuestions.Items.Count; j++)
                        {
                            string questionId = DataListQuestions.DataKeys[j].ToString();
                            singleScore = int.Parse(((TextBox)DataListQuestions.Items[j].FindControl("TxtWin")).Text.Trim());
                            //修改主观题得分
                            myExam.UpdateScoreOfObjectiveQuestions(HiddenExamId.Value, questionId,singleScore);
                            totalScore += singleScore;
                        }
                        break;
                }
            }
            if (myExam.SetWinScore(HiddenExamId.Value, totalScore, TxtComment.Text.Trim()))
            {
                LabPrompt.Text = "提交成功";
                LabMyScore.Text = totalScore.ToString();
            }
        }

        void BindHadMarkScore()
        {
            int i, j;
            for (i = 0; i < DataListBigQuestion.Items.Count; i++)
            {
                string itemType = DataListBigQuestion.DataKeys[i].ToString();
                DataList DataListQuestions = DataListBigQuestion.Items[i].FindControl("DataListQuestions") as DataList;
                for (j = 0; j < DataListQuestions.Items.Count; j++)
                {
                    string questionId = DataListQuestions.DataKeys[j].ToString();
                    TextBox TxtWin = (TextBox)DataListQuestions.Items[j].FindControl("TxtWin");
                    TxtWin.Text = myExam.GetRecordOfAnswer(HiddenExamId.Value, questionId).Rows[0]["score"].ToString();
                }
            }
        }
        //结束批阅，无法重复批阅
        protected void BtnEndMark_Click(object sender, EventArgs e)
        {
            if (myExam.EndMarkPaper(HiddenExamId.Value))
            {
                LabPrompt.Text = "批阅结束";
                BtnLogin.Visible = false;
                BtnEndMark.Visible = false;
            }
        }
    }
}
